package com.ray.service;

import com.ray.pojo.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class BeanDemoImpl
{
    public void getBeanUsingRef()
    {
        ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Object myStudent = cpx.getBean("myStudent");
        System.out.println(myStudent.toString());
    }

    public void getBeanUsingParamsConstructor()
    {
        ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = cpx.getBean("myStudentWithParamConstructor", Student.class);
        System.out.println(student.toString());
    }

    public void getBeanFromFileClass()
    {
        ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("applicationContext.xml");
        File myFile = cpx.getBean("myFile", File.class);

        try
        {
            InputStream in = new FileInputStream(myFile);
            //读取数据，并将读取的数据存储到数组中
            byte[] b = new byte[(int) myFile.length()];//数据存储的数组
            int len = 0;
            int temp = 0;
            while((temp = in.read()) != -1){//循环读取数据，未到达流的末尾
                b[len] = (byte) temp;//将有效数据存储在数组中
                len ++;
            }

            System.out.println(new String(b, 0, len, "GBK"));
            in.close();
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }

    public void getSingletonScope(){

        System.out.println("hello");
        // ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("applicationContext.xml");
        // Student myStudent = cpx.getBean("myStudent", Student.class);
        // Student myStudent2 = cpx.getBean("myStudent2", Student.class);
        // System.out.println(myStudent.getSchool() == myStudent2.getSchool());
    }
}
